#!/bin/bash
mkdir finished
# 遍历当前目录下所有以.deb结尾的文件
for deb in `ls ./*.deb`; do
  # 创建解包和重打包目录
  pkg_name=$(basename "$deb" .loongarch64.deb)
  data_dir="${pkg_name}_data"
  control_dir="${data_dir}/DEBIAN"

  # 使用dpkg -X解压数据内容
  dpkg -X "$deb" "$data_dir"
  # 使用dpkg -e解压控制文件
  dpkg -e "$deb" "$control_dir"

  # 修改控制文件中的架构为loong64
  sed -i 's/Architecture: .*/Architecture: loong64/' "$control_dir"/control
  # 在依赖列表中添加liblol和liblol-dkms
  if grep -q "Depends:" "$control_dir"/control; then
    sed -i '/Depends:/ s/$/, liblol,liblol-dkms/' "$control_dir"/control
  else
    echo "Depends: liblol" >> "$control_dir"/control
  fi

  # 使用dpkg-deb重新打包.deb
  fakeroot dpkg-deb -b "$data_dir" "finished/"

  # 清理
  rm -rf "$data_dir" 
done